package com.mos.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package Name: com.mos.util
 * User: grq
 * Date: 2016/4/7
 * Time: 15:18
 * Description:some des here!
 */
public class FileUtil {

    public static String getFileSuffix(String fileName) {
        if (StringUtils.isBlank(fileName)) return null;
        int index = fileName.lastIndexOf(".");
        if (-1 == index) return "";
        return fileName.substring(index + 1);
    }

    public static boolean copyFileToFile(String source,String desc)throws IOException {
        if (StringUtils.isBlank(source) || StringUtils.isBlank(desc))
            return false;

        BufferedInputStream bs = null;
        BufferedOutputStream os = null;
        try {
            File sourceFile = new File(source);

            if (!sourceFile.exists()) return false;

            File descFile = new File(desc);
            // 创建目录：new File("/tmp/one/two/three").mkdirs();执行后，会建立tmp/one/two/three四级目录
            // 创建目录：new File("/tmp/one/two/three").mkdir();执行后则不会建立任何目录，因为找不到/tmp/one/two目录
            // 创建文件：newFile("/tmp/one/two/three/aa").createNewFile;执行后，若/tmp/one/two/three/目录不存在，则文件会创建失败，不过我们一般不会显式的创建文件。
            // 判定文件或目录是否存在：exists;它可以判定文件，也可以判定目录。
            descFile.getParentFile().mkdirs();
            // descFile.createNewFile();没必要显式创建，以下会自动创建

            bs = new BufferedInputStream(new FileInputStream(sourceFile));
            os = new BufferedOutputStream(new FileOutputStream(descFile));

            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = bs.read(b)) != -1) {
                os.write(b, 0, len);
            }
            os.flush();

            return true;
        } finally {
            if (bs != null) {
                bs.close();
            }
            if (os != null) {
                os.close();
            }
        }

    }
    public static String getSmpleFileName(String fileName){
        File file=new File(fileName);
        return file.getName();
    }

    public static List<Map<String,String>> getSystemIcons(HttpServletRequest request){
        List<Map<String,String>> fileMaps = new ArrayList<Map<String, String>>();
        String iconPath = "statics\\images\\sys_icon";
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String path = realPath + iconPath;
        File root = new File(path);
        if (root.isDirectory()){
            File[] files = root.listFiles();
            for (int i=0;i<files.length;i++){
                Map<String,String> fileMap = new HashMap<String, String>();
                fileMap.put("name",files[i].getName());
                fileMap.put("path",files[i].getAbsolutePath());
                fileMap.put("showPath",files[i].getAbsolutePath().replace(realPath,"").replace("\\","/"));
                fileMaps.add(fileMap);
            }
        }
        return fileMaps;
    }

}
